﻿/******************************************************************************
 * Name         : LoginConnectManager.cpp
 * Author       : 671643387
 * Date         : 2015年9月14日
 * Description  :
 *****************************************************************************/

#include <boost/bind.hpp>
#include <dev/base/Runtime.hpp>
#include <dev/base/detail/XmlConfigFile.hpp>
#include <common/logger/Logger.hpp>
#include "../Config.hpp"
#include "../main/LSMain.hpp"
#include "../player/LoginPlayerManager.hpp"
#include "../net/LoginConnectionFactory.hpp"
#include "LoginConnectManager.hpp"
using namespace sxr::login_server;

static LoginConnectManager sLoginConnectManager;

LoginConnectManager* LoginConnectManager::getMe()
{
	return &sLoginConnectManager;
}

LoginConnectManager::LoginConnectManager()
	: eventLoop_()
	, tcpServer_(eventLoop_)
{
	tcpServer_.setConnecionFactory(dev::net::TcpConnectionFactoryPtr(new LoginConnectionFactory()));

    /*
	 * tcpServer_.setMessageCallback(boost::bind(&LoginConnectManager::onMessage, this, _1, _2));
	 * tcpServer_.setConnectionErrorCallback(boost::bind(&LoginConnectManager::onConnectionError, this, _1));
	 * tcpServer_.setConnectionClosingCallback(boost::bind(&LoginConnectManager::onConnectionClosing, this, _1));
	 * tcpServer_.setConnectionHeartBeatCallback(boost::bind(&LoginConnectManager::onConnectionHeartBeat, this, _1));
	 * tcpServer_.setConnectionWritableCallback(boost::bind(&LoginConnectManager::onConnectionWritable, this, _1));
     */
	tcpServer_.setConnectionEstablishedCallback(boost::bind(&LoginConnectManager::onConnectionEstablished, this, _1));

	tcpServer_.setServerShutdownCallback(boost::bind(&LoginConnectManager::onServerShutdown, this));
}

LoginConnectManager::~LoginConnectManager()
{

}

bool LoginConnectManager::initialize(void)
{
	__DV_TRY
	{
		// 初始化登录玩家管理器
		if (!LoginPlayerManager::getMe()->initialize())
		{
            sxr::common::getLogger()->log("LoginPlayerManager::initialize...[FAIL]");
			return false;
		}
        sxr::common::getLogger()->log("LoginPlayerManager::initialize... [OK]");

		//// 初始化TCP服务器
		dev::base::detail::XmlConfigFile configFile("Login.xml");
        // 配置TCP服务器
        tcpServer_.config(dev::net::TcpServer::CONNISSIZE, LOGIN_TCP_CONNECTION_IS_BUFSIZE);
        tcpServer_.config(dev::net::TcpServer::CONNOSSIZE, LOGIN_TCP_CONNECTION_OS_BUFSIZE);
        tcpServer_.config(dev::net::TcpServer::WORKERNUM, LOGIN_CONNECT_MANAGER_WORKERS);
        tcpServer_.config(dev::net::TcpServer::LOGGER, sxr::common::getLogger());
        // 启动TCP服务器
		if (!tcpServer_.open(configFile.get<std::string>("Server.Ip").c_str(), 
			configFile.get<int>("Server.Port"), configFile.get<int>("Server.Backlog")))
		{
			return false;
		}
	}
	__DV_CATCH(std::exception& e)
	{
		__DV_DUMP(e);
	}
	__DV_CATCH(...)
	{

	}

	return true;
}

void LoginConnectManager::stop()
{
	tcpServer_.close();
}

void LoginConnectManager::run()
{
	eventLoop_.run();
}

///////////////////////////////////////////////////////////////////////////////

void LoginConnectManager::onServerShutdown()
{
    // 网络服务器关闭
	eventLoop_.stop();

    // 清理登录玩家管理器
    LoginPlayerManager::getMe()->cleanup();
	sxr::common::getLogger()->log("LoginPlayerManager::cleanup...[OK]");
}

void LoginConnectManager::onConnectionEstablished(dev::net::TcpConnectionPtr& conn)
{
	if (!LoginPlayerManager::getMe()->newPlayer(conn))
	{
		conn->shutdown();
	}
}

///////////////////////////////////////////////////////////////////////////////
/*
void LoginConnectManager::onConnectionError(dev::net::TcpConnection& conn)
{
}
void LoginConnectManager::onConnectionClosing(dev::net::TcpConnection& conn)
{
}
void LoginConnectManager::onConnectionHeartBeat(dev::net::TcpConnection& conn)
{
}
void LoginConnectManager::onConnectionWritable(dev::net::TcpConnection& conn)
{
}
void LoginConnectManager::onMessage(dev::net::TcpConnection& conn, dev::net::TcpConnectionInputStream& is)
{
}
*/

